<?php

/**
 * Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
 * Copyright (c) 2010 Mr. Ynk: http://www.mrynk.nl
 */

/**
 * @author Michel Ypma: michel@mrynk.nl
 */

require_once 'interfaces/iSyntax.php';

class Syntax implements iSyntax
{
    const PATTERN_S             = '/\{(([^\{\}]*):{1})?';
    const PATTERN_E             = '(:{1}([^\{\}]*))?\}/';

    private $template;
    private $templates;
    private $languages;
    private $childs;
    private $child_groups;
    private $attribute;


    public function __construct($template, $languages, $attribute = false)
    {
        $this->template         = $template;
        $this->templates        = array();
        $this->languages        = $languages;
        $this->childs           = array();
        $this->child_groups     = array();
        $this->attribute        = $attribute;

        foreach ($this->languages as $language)
        {
            $this->templates[(string) $language['name']] = file_get_contents(MYTO_LIB . 'builder/sntx/'.trim($language['name']).'/'.$this->template.'.sntx');
        }

        if ($this->attribute)
        {
            foreach ($this->attribute->attributes() as $search => $replace)
            {
                $this->set($search, $replace);
            }
        }
    }

    public function set($search, $replace)
    {
        foreach ($this->templates as $k => $template)
        {
            $this->templates[$k] = preg_replace(self::PATTERN_S.$search.self::PATTERN_E, '$2'.$replace.'$4', $this->templates[$k]);
        }
    }

    public function newChild($template, $attribute = false)
    {
        return new Syntax($template, $this->languages, $attribute);
    }

    public function addChild(Syntax $child, $group = 'nest')
    {
        if (!in_array($group, $this->child_groups))
        {
            $this->child_groups[] = $group;
        }

        $this->childs[$group][] = $child;
    }

    public function getChild($template)
    {
        foreach ($this->childs as $child)
        {
            foreach ($child as $c)
            {
                if ($c->getTemplate() == $template)
                {
                    return $c;
                }
            }
        }

        return false;
    }

    public function getTemplate()
    {
        return $this->template;
    }

    public function getContents()
    {
        $arr = array();

        foreach ($this->templates as $language => $template)
        {
            $arr[$language] = $template;

            foreach ($this->child_groups as $child_group)
            {
                if (preg_match(self::PATTERN_S.$child_group.self::PATTERN_E, $arr[$language]))
                {
                    $str = '';

                    foreach ($this->childs[$child_group] as $k => $child)
                    {
                        $contents = $child->getContents();

                        if ($k > 0) { $str .= "\n\n"; }

                        $str .= $contents[$language];
                    }

                    $arr[$language] = str_replace('{'.$child_group.'}', $str, $arr[$language]);
                }
            }

            $arr[$language] = preg_replace(self::PATTERN_S.'.*'.self::PATTERN_E, '', $arr[$language]);

            $arr[$language] = trim($arr[$language]);
        }

        return $arr;
    }
}
?>